﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using WpfApp4.Models;

namespace WpfApp4.ViewModels
{
    public class TestWindowViewModel : ViewModelBase
    {
        public ObservableCollection<FileSystemNode> MyTreeViewItems { get; }

        public TestWindowViewModel()
        {
            MyTreeViewItems = new ObservableCollection<FileSystemNode>();
            LoadFileSystem(MyTreeViewItems, @"d:\Desktop\DBC\");
        }

        private void LoadFileSystem(ObservableCollection<FileSystemNode> collection, string path)
        {
            string[] directories = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);

            foreach (string directory in directories)
            {
                var fsn = new FileSystemNode { Name = Path.GetFileName(directory), IsFile = false };
                fsn.Children = new ObservableCollection<FileSystemNode>();

                collection.Add(fsn);
                LoadFileSystem(fsn.Children,directory);
            }

            foreach (string file in files)
            {
                collection.Add(new FileSystemNode { Name = Path.GetFileName(file), IsFile = true });
            }
        }
    }

    public class FileSystemNode
    {
        public string Name { get; set; }
        public bool IsFile { get; set; }
        public ObservableCollection<FileSystemNode> Children { get; set; }
    }
}
